<?php
/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/18
 * Time: 15:55
 * 商品管理
 */
class goods extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

        $this->load->model('goods_model');
    }

    /**
     * 商品列表
     */
    public function index(){

        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->goods_model->search('*',0,$searchArg,$page_num,$start_position);
//            print_r($data);
//            die();
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('/admin/goods/list',$viewData);
        } else {

//            print_r(123);
//            die();
            $data = $this->goods_model->page('goods',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('/admin/goods/list',$viewData);
        }

    }

    //添加功能
    public function add(){

        $data = [
            'username' => $this->input->post('username'),
            'nickname' => $this->input->post('nickname'),
            'password' => $this->input->post('password'),
            'loginTime' => time(),
        ];

        //是否会有添加失败的情况?

        $resultData =  $this->user_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/user/index');
        }
    }

    //添加页面
    public function addView(){

        $this->load->view('/admin/goods/add');
    }

    //编辑功能
    public function edit(){
        // 上传文件到服务器目录
        $config['upload_path'] = './upload/goods';
        // 允许上传哪些类型
        $config['allowed_types'] = 'gif|png|jpg|jpeg';
        // 上传后的文件名，用uniqid()保证文件名唯一
        $config['file_name'] = uniqid();

        // 加载上传库
        $this->load->library('upload', $config);
        // 上传文件，这里的pic是视图中file控件的name属性
        $result = $this->upload->do_upload('image1');
//        var_dump($result);
        // 如果上传成功，获取上传文件的信息
        if ($result)
        {
            var_dump($this->upload->data());
        }
//        print_r($_FILES);
        //用户id
//        $where = [
//            'id' => $this->input->post('id')
//        ];
//        //todo 更多字段
//        $data = [
//            'nickname' => $this->input->post('nickname'),
//        ];
//
//        //是否会有添加失败的情况?
//        $resultData =  $this->goods_model->edit($where,$data);
//
//        if($resultData){
//            echo '<script>alert("修改成功")</script>';
//            redirect('/admin/goods/index');
//        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $result['goodsList'] = $this->goods_model->getByFieldQueryData('*',$arg,0);

        $this->load->view('/admin/goods/edit',$result);
    }

    //删除功能
    public function remove(){

        $postData = $this->input->post();

        if ($postData){
            if(!$postData){
                redirect('/admin/goods/index');
            }

            $post = $this->input->post('del_id');
//            print_r($post);
//            die();
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->goods_model->remove($where);
            }

            redirect('/admin/goods/index');
        } else {
            $where = [
                'id' => $this->input->get('id')
            ];

            //是否会有添加失败的情况?
            $this->goods_model->remove($where);
            redirect('/admin/goods/index');

        }

//        //是否会有添加失败的情况?
//        $resultData =  $this->goods_model->remove($data);
//        if($resultData){
//            echo '<script>alert("删除成功")</script>';
//            redirect('/admin/goods/index');
//        }
    }

    public function category()
    {
        $this->load->view('/admin/goods/category');

    }
}